<template>
  <div class="settings-panel" v-if="isOpen">
    <div class="settings-header">
      <h3>编辑器设置</h3>
      <button class="close-btn" @click="$emit('close')">×</button>
    </div>
    <div class="settings-content">
      <div class="setting-group">
        <label>主题</label>
        <select v-model="settings.theme">
          <option value="light">浅色</option>
          <option value="dark">深色</option>
        </select>
      </div>

      <div class="setting-group">
        <label>字体大小</label>
        <div class="font-size-control">
          <button @click="decreaseFontSize">-</button>
          <span>{{ settings.fontSize }}px</span>
          <button @click="increaseFontSize">+</button>
        </div>
      </div>

      <div class="setting-group">
        <label>自动保存</label>
        <input type="checkbox" v-model="settings.autoSave" />
      </div>
    </div>

    <div class="settings-footer">
      <button class="apply-btn" @click="applySettings">应用</button>
      <button class="cancel-btn" @click="$emit('close')">取消</button>
    </div>
  </div>
</template>

<script setup>
import { ref, reactive } from "vue";

const props = defineProps({
  isOpen: {
    type: Boolean,
    default: false,
  },
  initialSettings: {
    type: Object,
    default: () => ({
      theme: "dark",
      fontSize: 14,
      autoSave: true,
    }),
  },
});

const emit = defineEmits(["close", "apply"]);

const settings = reactive({
  theme: props.initialSettings.theme,
  fontSize: props.initialSettings.fontSize,
  autoSave: props.initialSettings.autoSave,
});

function increaseFontSize() {
  if (settings.fontSize < 24) {
    settings.fontSize += 1;
  }
}

function decreaseFontSize() {
  if (settings.fontSize > 10) {
    settings.fontSize -= 1;
  }
}

function applySettings() {
  emit("apply", { ...settings });
  emit("close");
}
</script>

<style scoped>
.settings-panel {
  position: fixed;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  width: 350px;
  background-color: #252525;
  color: #f0f0f0;
  border-radius: 6px;
  box-shadow: 0 5px 15px rgba(0, 0, 0, 0.5);
  z-index: 1000;
}

.settings-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 12px 16px;
  border-bottom: 1px solid #333;
}

.settings-header h3 {
  margin: 0;
  font-size: 16px;
  font-weight: 500;
}

.close-btn {
  background: none;
  border: none;
  color: #aaa;
  font-size: 20px;
  cursor: pointer;
}

.close-btn:hover {
  color: #fff;
}

.settings-content {
  padding: 16px;
}

.setting-group {
  margin-bottom: 16px;
}

.setting-group label {
  display: block;
  margin-bottom: 8px;
  font-size: 14px;
  color: #aaa;
}

.setting-group select,
.setting-group input[type="text"] {
  width: 100%;
  padding: 8px;
  background-color: #333;
  color: #fff;
  border: 1px solid #444;
  border-radius: 4px;
}

.font-size-control {
  display: flex;
  align-items: center;
}

.font-size-control button {
  width: 30px;
  height: 30px;
  background-color: #333;
  color: #fff;
  border: 1px solid #444;
  border-radius: 4px;
  cursor: pointer;
}

.font-size-control span {
  margin: 0 12px;
  min-width: 40px;
  text-align: center;
}

.settings-footer {
  display: flex;
  justify-content: flex-end;
  padding: 12px 16px;
  border-top: 1px solid #333;
}

.apply-btn,
.cancel-btn {
  padding: 6px 12px;
  border: none;
  border-radius: 4px;
  font-size: 14px;
  cursor: pointer;
  margin-left: 8px;
}

.apply-btn {
  background-color: #0078d7;
  color: #fff;
}

.apply-btn:hover {
  background-color: #0086f0;
}

.cancel-btn {
  background-color: #444;
  color: #fff;
}

.cancel-btn:hover {
  background-color: #555;
}
</style>
